package com.blrs.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.blrs.VO.CooperateExportVO;
import com.blrs.common.constant.Constants;
import com.blrs.service.procure.IProcureCooperateService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.blrs.common.annotation.Log;
import com.blrs.common.core.controller.BaseController;
import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.enums.BusinessType;
import com.blrs.domain.ProcureCooperate;
import com.blrs.common.utils.poi.ExcelUtil;
import com.blrs.common.core.page.TableDataInfo;

/**
 * 采购与合作Controller
 * 
 * @author ruoyi
 * @date 2023-08-01
 */
@RestController
@RequestMapping("/cooperate/cooperate")
public class ProcureCooperateController extends BaseController
{
    @Autowired
    private IProcureCooperateService procureCooperateService;

    /**
     * 查询采购列表
     */
    @GetMapping("/procureList")
    public TableDataInfo list(ProcureCooperate procureCooperate)
    {
        startPage();
        procureCooperate.setProcureCooperate(Constants.PROCURE);
        List<ProcureCooperate> list = procureCooperateService.selectProcureCooperateList(procureCooperate);
         return getDataTable(list);
    }
    /**
     * 查询采购列表
     */
    @GetMapping("/cooperateList")
    public TableDataInfo cooperateList(ProcureCooperate procureCooperate)
    {
        startPage();
        procureCooperate.setProcureCooperate(Constants.COOPERATE);
        List<ProcureCooperate> list = procureCooperateService.selectProcureCooperateList(procureCooperate);
        return getDataTable(list);
    }

    /**
     * 导出采购列表
     */
    @PreAuthorize("@ss.hasPermi('cooperate:cooperate:export')")
    @Log(title = "采购与合作", businessType = BusinessType.EXPORT)
    @PostMapping("/exportProcure")
    public void exportProcure(HttpServletResponse response, ProcureCooperate procureCooperate)
    {
        procureCooperate.setProcureCooperate(Constants.PROCURE);
        List<ProcureCooperate> list = procureCooperateService.selectProcureCooperateList(procureCooperate);
        ExcelUtil<ProcureCooperate> util = new ExcelUtil<ProcureCooperate>(ProcureCooperate.class);
        util.exportExcel(response, list, "采购信息数据");
    }
    /**
     * 导出合作列表
     */
    @PreAuthorize("@ss.hasPermi('cooperate:cooperate:export')")
    @Log(title = "采购与合作", businessType = BusinessType.EXPORT)
    @PostMapping("/exportCooperate")
    public void exportCooperate(HttpServletResponse response, ProcureCooperate procureCooperate)
    {
        procureCooperate.setProcureCooperate(Constants.COOPERATE);
        List<ProcureCooperate> list = procureCooperateService.selectProcureCooperateList(procureCooperate);
        List<CooperateExportVO> targetList = list.stream()
                .map(sourceObj -> {
                    CooperateExportVO targetObj = new CooperateExportVO();
                    // 执行属性拷贝操作，可以使用 BeanUtils.copyProperties 或手动设置属性值
                     BeanUtils.copyProperties(sourceObj, targetObj);
                    return targetObj;
                })
                .collect(Collectors.toList());
        ExcelUtil<CooperateExportVO> util = new ExcelUtil<CooperateExportVO>(CooperateExportVO.class);
        util.exportExcel(response, targetList, "合作信息数据");
    }

    /**
     * 获取采购与合作详细信息
     */
    @PreAuthorize("@ss.hasPermi('cooperate:cooperate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(procureCooperateService.selectProcureCooperateById(id));
    }

    /**
     * 新增采购与合作
     */
    @PreAuthorize("@ss.hasPermi('cooperate:cooperate:add')")
    @Log(title = "采购与合作", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProcureCooperate procureCooperate)
    {
        return toAjax(procureCooperateService.insertProcureCooperate(procureCooperate));
    }

    /**
     * 修改采购与合作
     */
    @PreAuthorize("@ss.hasPermi('cooperate:cooperate:edit')")
    @Log(title = "采购与合作", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProcureCooperate procureCooperate)
    {
        return toAjax(procureCooperateService.updateProcureCooperate(procureCooperate));
    }

}
